#!/usr/bin/python
# -*- coding: utf-8 -*-  
"""
@Project : hello 
@file : oracle_to_csv.py
@Author : shenj
@time : 2025/5/22 11:05
@func :
"""
from com.cn.for_cdc.check_data.oracle_to_csv_tmp import write_large_csv

# !/usr/bin/python
# -*- coding: utf-8 -*-
"""
@Project : data_export
@file : oracle_to_csv_optimized.py
@desc : 高效Oracle数据导出工具（支持百万级数据）
"""
import logging
import time
from pathlib import Path
from typing import Generator, List, Dict

from com.cn.for_cdc.common.OracleQueryExecutor import OracleQueryExecutor
from com.cn.for_cdc.common.cdc_conmons import oracle_adm_connections
from com.cn.for_cdc.common.log_helper import configure_logging


def export_data(config: Dict, sql: str, output_dir: str) -> str:
    """数据导出主流程"""
    # 初始化组件
    executor = OracleQueryExecutor(config)

    # 生成带时间戳的文件名
    timestamp = time.strftime("%Y%m%d_%H%M%S")
    filename = Path(output_dir) / f"icinl_voucher_{timestamp}.csv"

    try:
        # 分阶段计时
        timer = {
            'query': time.time(),
            'write': None,
            'total': time.time()
        }

        # 分批获取数据
        data_gen = executor.execute_query(sql)

        # 流式写入CSV
        timer['write'] = time.time()
        write_large_csv(data_gen, str(filename))

        # 记录性能指标
        time_cost = {
            'query': round(time.time() - timer['query'], 2),
            'write': round(time.time() - timer['write'], 2),
            'total': round(time.time() - timer['total'], 2)
        }

        logging.info(
            f"导出完成！耗时统计(秒)-查询:{time_cost['query']} "
            f"写入:{time_cost['write']} 总计:{time_cost['total']}"
        )
        return str(filename)

    except Exception as e:
        logging.error(f"数据导出失败: {str(e)}", exc_info=True)
        raise
    finally:
        executor.close()


if __name__ == "__main__":
    configure_logging()

    # 配置参数
    CONFIG = oracle_adm_connections['icibe_pro']
    OUTPUT_DIR = r"F:\workspace\pythonProject\com\cn\for_cdc\output_data"

    COMPLEX_SQL = """
    
    SELECT /*+ parallel(8) */ 
        a.row_id, a.member_id, a.X_VOUCHER_VAL, 
        a.STATUS_CD, a.USED_DT
    FROM SIEBEL.S_LOY_MEM_VCHR@SBL a
    JOIN siebel.s_loy_member@SBL b 
        ON a.member_id = b.row_id
    WHERE b.bu_id = '1-7V7X'
        AND a.LAST_UPD < SYSDATE - 1 
    MINUS
    SELECT 
        VOUCHER_ID, MEMBER_ID, VOUCHER_VAL,
        VOUCHER_STATUS_CODE, VOUCHER_USED_DT 
    FROM STG_B_MEM_VOUCHER 
    WHERE BU_CODE = 'ICIBE'
    
    """

    try:
        result_file = export_data(CONFIG, COMPLEX_SQL, OUTPUT_DIR)
        logging.info(f"数据已导出至: {result_file}")
    except Exception as e:
        logging.error("数据导出流程异常终止")


